package com.app.bussinesslocator.adapter;

import java.util.ArrayList;

import com.app.bussinesslocator.R;
import com.app.bussinesslocator.bean.ObjBussiness;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

public class ResultAdapter extends ArrayAdapter<ObjBussiness> {
	
	Context context;
	ArrayList<ObjBussiness> objBussinesses = new ArrayList<ObjBussiness>();

	public ResultAdapter(Context context, ArrayList<ObjBussiness> objBussinesses) {
		super(context, R.layout.result_item, objBussinesses);
		this.context = context;
		this.objBussinesses = objBussinesses;
	}

	@Override
	public View getView(int position, View v, ViewGroup parent) {
		Holder holder = null;
		if (v==null){
			LayoutInflater inflater = LayoutInflater.from(getContext());
			v = inflater.inflate(R.layout.result_item, parent, false);
			holder = new Holder();
			
			holder.nameBusiness = (TextView) v.findViewById(R.id.txt_name_business);	
			holder.distance = (TextView) v.findViewById(R.id.txt_distance);
			holder.cost = (TextView) v.findViewById(R.id.txt_cost);
			holder.ratingBar = (RatingBar) v.findViewById(R.id.ratingBar);
			
			v.setTag(holder);
		} else {
			holder = (Holder) v.getTag();
		}
		
		ObjBussiness objBussiness = objBussinesses.get(position);
		
		if (objBussiness.getBussinessName().length() > 30){
			holder.nameBusiness.setText(objBussiness.getBussinessName().substring(0, 30) + "...");
		} else {
			holder.nameBusiness.setText(objBussiness.getBussinessName());
		}
		
		/*if (objBussiness.getDistance() > 1) {
			holder.distance.setText(objBussiness.getDistance()+"km");
		} else {
			holder.distance.setText("< 1km");
		}*/
		holder.distance.setText(objBussiness.getDistance()+"km");
		
		
		holder.cost.setText("$"+objBussiness.getPrice());
		holder.ratingBar.setRating(objBussiness.getOverall());
		
		
		return v;
	}
	
	
	@Override
	public ObjBussiness getItem(int position) {
		return objBussinesses.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	


	@Override
	public int getCount() {
		return objBussinesses.size();
	}




	static class Holder{
		TextView nameBusiness;
		TextView distance;
		TextView cost;
		RatingBar ratingBar;
	}

}
